package com.xuhj.array;

import java.util.Arrays;
/*
 * @Author xuhj
 * @Description https://leetcode-cn.com/problems/spiral-matrix-ii/
 * @Date 11:14 下午 2022/1/21
 * @Param 
 * @return 
 **/
public class LeetCode_59 {
    public static void main(String[] args) {
        int[][] ints = printArray(4);
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                if(j < 3){
                    System.out.print(ints[i][j] + " ");
                }else {
                    System.out.println(ints[i][j]);
                }
            }
        }

    }
    public static int[][] printArray(int k){
        int[][] waitForArray = new int[k][k];
        int number = 1;
        int left = 0;
        int right = k - 1;
        int bottom = k - 1;
        int top = 0;
        while(number <= k * k){
            // left -> right
            for(int i = left; i <= right; i++){
                waitForArray[top][i] = number++;
            }
            top++;
            // top -> bottom
            for(int i = top; i <= bottom; i++){
                waitForArray[i][right] = number++;
            }
            right--;
            // right -> left
            for(int i = right; i >= left; i--){
                waitForArray[bottom][i] = number++;
            }
            bottom--;
            // bottom -> top
            for(int i = bottom; i >= top; i--){
                waitForArray[i][left] = number++;
            }
            left++;
        }
        return waitForArray;
    }
}
/**
 * 螺旋矩阵
 * 输入：n = 3
 * 输出：[[1,2,3],[8,9,4],[7,6,5]]
*/
